
#include "BStreamExtractor.h"
#include "Extractor.h"
#include <string>

using namespace std;
/*
int main( int argc, char** argv)
{

	//char* i = "/home/hduser/Videos/svc/earth.svc";
	//char* o = getOutputFileName(i, 1);
	//printf("%s", o);

	//return 0;

  Extractor*          pcExtractor = NULL;
  ExtractorParameter  cParameter;
  UInt iLayerIds[MAX_LAYERS];
  UInt iNumLayersMinus1 = 0;
  //additions
  string hostnames[]={"localhost","localhost","localhost"};
  string portnos[]={"6001","6002","6003"};

  printf( "JSVM %s BitStream Extractor \n\n", _JSVM_VERSION_ );

  RNOKRS( cParameter.init       ( argc, argv ),   -2 );

  for( Int n = 0; n < 1; n++ )
  {
    RNOKR( Extractor::create    ( pcExtractor ),  -3 );
    cParameter.hostname=(char *)hostnames[n].c_str();
    cParameter.portno=(char *)portnos[n].c_str();
    cerr<<"cParameter hostname is :"<<cParameter.hostname<<endl;
    cerr<<"cParameter portno is :"<<cParameter.portno<<endl;
    RNOKR( pcExtractor->init    ( &cParameter ),  -4 );

   RNOKR( pcExtractor->go      (),               -5 );

    RNOKR( pcExtractor->destroy (),               -6 );
  }

  return 0;
}
*/



char* getOutputFileName(char* ip, UInt ilayerId);

int main( int argc, char** argv)
{

	//char* i = "/home/hduser/Videos/svc/earth.svc";
	//char* o = getOutputFileName(i, 1);
	//printf("%s", o);

	//return 0;

   //additions
  string hostnames[]={"localhost","localhost","localhost"};
  string portnos[]={"6001","6002","6003"};

  Extractor*          pcExtractor = NULL;
  ExtractorParameter  cParameter;
  UInt iLayerIds[MAX_LAYERS];
  UInt iNumLayersMinus1 = 0;

  printf( "JSVM %s BitStream Extractor \n\n", _JSVM_VERSION_ );

  RNOKRS( cParameter.init       ( argc, argv ),   -2 );

  for( Int n = 0; n < 1; n++ )
  {
    RNOKR( Extractor::create    ( pcExtractor ),  -3 );

    RNOKR( pcExtractor->init    ( &cParameter ),  -4 );

    RNOKR( pcExtractor->go      (),               -5 );

    iNumLayersMinus1 = pcExtractor->getExtNumLayersMinus1();
    for (UInt i = 0; i<=iNumLayersMinus1; i++)
    {
    	iLayerIds[i] = pcExtractor->getExtLayerId(i);
    }

    for(UInt i = 0; i<=iNumLayersMinus1; i++)
    {
    	printf("%3d\n",iLayerIds[i]);
    }

    RNOKR( pcExtractor->destroy (),               -6 );
  }

  for (int iCtr = 0; iCtr<=iNumLayersMinus1; iCtr++)
  {
	  cerr<<"input file name :"<<argv[1]<<endl;

	  argv[2] = getOutputFileName(argv[1], iLayerIds[iCtr]);
	  cerr<<"output file name :"<<argv[2]<<endl;
	  argv[3] = "-sl";
	  sprintf(argv[4], "%d", iLayerIds[iCtr]);

	  argc = 5;

	  pcExtractor = NULL;
	  //cParameter = NULL;

	 // printf( "Calling again \n\n");

	  RNOKRS( cParameter.init       ( argc, argv ),   -2 );

	  for( Int n = 0; n < 1; n++ )
	  {
		RNOKR( Extractor::create    ( pcExtractor ),  -3 );
		cParameter.hostname=(char *)hostnames[iCtr].c_str();
		cParameter.portno=(char *)portnos[iCtr].c_str();
		cerr<<"cParameter hostname is :"<<cParameter.hostname<<endl;
		cerr<<"cParameter portno is :"<<cParameter.portno<<endl;

		RNOKR( pcExtractor->init    ( &cParameter ),  -4 );

		RNOKR( pcExtractor->go      (),               -5 );

		RNOKR( pcExtractor->destroy (),               -6 );
	  }

  }

  return 0;
}

char*
getOutputFileName(char* ip, UInt ilayerId)
{
	string sip(ip);
	int ipos = sip.rfind('.');
	int ilength = sip.length();

	string sop = sip.replace(ipos, (ilength - ipos), "");

	char slayerid[10];
	sprintf(slayerid, "_%d.svc", ilayerId);

	sop.append(slayerid);

	return (char*)sop.c_str();
}

